package com.bidcms.web.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bidcms.dao.attachmentMapper;
import com.bidcms.dao.componentMapper;
import com.bidcms.dao.featureMapper;
import com.bidcms.dao.feature_classMapper;
import com.bidcms.domain.attachment;
import com.bidcms.domain.component;
import com.bidcms.domain.feature;
import com.bidcms.domain.feature_class;
import com.bidcms.util.JsonToFile;
import com.bidcms.util.MyDateUtil;
import com.bidcms.util.PageHtmlStrUtil;
import com.bidcms.web.BaseController;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import common.utils.DateUtils;
import freemarker.template.SimpleScalar;

@Controller
@RequestMapping("models")
public class showcase extends BaseController {

	@Autowired
	private componentMapper componentDao;
	@Autowired
	private featureMapper featureDao;
	@Autowired
	private feature_classMapper featureclassDao;
	@Autowired
	private attachmentMapper attachmentDao;
	
//Map<String, Object> params = new HashMap<String, Object>();

	@ResponseBody
	@RequestMapping(value = "showcase/category/item", method = RequestMethod.POST)
	public String category_item_post(HttpServletRequest req,
			HttpServletResponse resp, Integer is_display, String data) {
		// data:[{"type":"config","title":"现在出现在","first_priority":"0","second_priority":"1","show_method":"0","content":"","is_default":"0"}]
		// is_display:0
		JsonParser jsonParse = new JsonParser();
		JsonObject json = new JsonObject();
		JsonArray jsona = new JsonArray();
		jsona = (JsonArray) jsonParse.parse(data);
		json = (JsonObject) jsona.get(0);
		feature_class fc = new feature_class();
		fc.setTitle(json.get("title").getAsString());
		fc.setCreatedTime(Integer.valueOf(new Long(new Date().getTime())
				.toString().substring(0, 10)));
		fc.setFeatureCount(0);
		fc.setStoreId(1);
		Byte bte = new Byte((byte) 0);
		fc.setIsDefault(bte);
		String alias = new Long(System.nanoTime()).toString();
		fc.setAlias(alias);
		featureclassDao.insertSelective(fc);
		Integer id = fc.getId();
		if (id > 0) {
			String datapath = req.getSession().getServletContext()
					.getRealPath("/data");
			JsonToFile.savefeature_class(id.toString(), data, datapath);
		}
		json = new JsonObject();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject datajson = new JsonObject();
		datajson.addProperty("alias", alias);
		datajson.addProperty("id", id.toString());
		;
		json.add("data", datajson);
		// '{"code":0,"msg":"","data":{"alias":"'.$alias.'","id":"'.$id.'"}}';
		return json.toString();
	}

	@ResponseBody
	@RequestMapping(value = "showcase/category/item", method = RequestMethod.PUT)
	public String category_item_put(HttpServletRequest req,
			HttpServletResponse resp,Integer id,String data,String alias) {
		JsonParser jsonParse = new JsonParser();
		JsonObject json = new JsonObject();
		JsonArray jsona = new JsonArray();
		jsona = (JsonArray) jsonParse.parse(data);
		json = (JsonObject) jsona.get(0);
		String title = json.get("title").getAsString();
		if(id>0&&StringUtils.hasText(title)&&StringUtils.hasText(alias)){
			feature_class fc = new feature_class();
			fc.setTitle(title);
			fc.setId(id);
			featureclassDao.updateByPrimaryKeySelective(fc);
			JsonToFile.savefeature_class(id.toString(), data, getRealPath(req, "/data"));
		}
		json = new JsonObject();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject jsondata = new JsonObject();
		jsondata.addProperty("alias", alias);
		jsondata.addProperty("id", id.toString());
		json.add("data",jsondata);
		//'{"code":0,"msg":"","data":{"alias":"'.$alias.'","id":"'.$id.'"}}';
		return json.toString();
	}

	@ResponseBody
	@RequestMapping(value = "showcase/category/item", method = RequestMethod.DELETE)
	public String category_item_delete(HttpServletRequest req,
			HttpServletResponse resp,Integer id,String[] ids) {
		List<Integer> idList = new ArrayList<Integer>();
		if(id!=null){
			idList.add(id);
		}
		if(ids!=null){
			for(String idCu:ids){
				idList.add(Integer.valueOf(idCu));
			}
		}
		for(int i =0;i<idList.size();i++){
			featureclassDao.deleteByPrimaryKey(idList.get(i));
			JsonToFile.deletefeature_classjson(idList.get(i).toString(), getRealPath(req, "/data"));
		}
		JsonObject json = new JsonObject();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		json.addProperty("data", true);
		return json.toString();
	}

	@ResponseBody
	@RequestMapping(value = "showcase/category/item", method = RequestMethod.GET)
	public String category_item_get(HttpServletRequest req,
			HttpServletResponse resp,Integer id) {
		feature_class fc = featureclassDao.selectByPrimaryKey(id);
		JsonObject json = new JsonObject();
		JsonParser jsonp = new JsonParser();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject data = new JsonObject();
		data.addProperty("id", fc.getId().toString());
		data.addProperty("alias", fc.getAlias());
		data.add("data", jsonp.parse(JsonToFile.getfeature_classjson(fc.getId().toString(), getRealPath(req,"/data"))));
		data.addProperty("is_default", fc.getIsDefault());
		json.add("data",data );
		return json.toString();
	}

	// category/option
	// models/showcase/category/list
	@ResponseBody
	@RequestMapping(value = "showcase/category/list", method = RequestMethod.GET)
	public String category_item_list(HttpServletRequest req,
			HttpServletResponse resp,Integer is_display,
			String orderby, String order, String keyword,Integer p) {
		JsonObject json = new JsonObject();
		JsonObject data = new JsonObject();
		JsonArray items = new JsonArray();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("store_id", 1);
		params.put("orderby", orderby);
		params.put("order", order);
		if (StringUtils.hasText(keyword)) {
			params.put("keyword", keyword);
			// $sql.=" and  title like '%".$keyword."%'";
		}
		params.put("p", p);
		long total = featureclassDao.queryPage(params);
		PageHtmlStrUtil phs = new PageHtmlStrUtil(total,p,20);
		addPageParams(p, 20, params);
		List<feature_class> fcs = featureclassDao.findByMap(params);
		for (feature_class fc : fcs) {
			JsonObject item = new JsonObject();
			item.addProperty("item_id", fc.getId().toString());
			item.addProperty("title", fc.getTitle());
			item.addProperty("alias", fc.getAlias());
			item.addProperty("feature_count", fc.getFeatureCount().toString());
			item.addProperty("created_time", MyDateUtil.IntegerToDateString(fc.getCreatedTime()));
			item.addProperty("is_default", fc.getIsDefault().toString());
			item.addProperty("url", getFreeMarkerVar("webRoot")
					+ "/index?con=show&act=fcategory&id=" + fc.getId());
			items.add(item);
		}
		data.add("list", items);
		data.addProperty("page", phs.displayForPage());
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		json.add("data", data);
		return json.toString();
	}

	// category/option
	@ResponseBody
	@RequestMapping(value = "showcase/category/option", method = RequestMethod.GET)
	public String category_item_option(HttpServletRequest req,
			HttpServletResponse resp) {
		StringBuilder sb = new StringBuilder();
		List<feature_class> fcs = featureclassDao.findAll();
		for (feature_class fc : fcs) {
			sb.append("<option value=\"");
			sb.append(fc.getId());
			sb.append("\">");
			sb.append(fc.getTitle());
			sb.append("</option>");
		}
		//setHeader("Content-Type", "application/html;charset=UTF-8");
		return sb.toString();
	}

	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/category", method = RequestMethod.PUT)
	public String feature_category(HttpServletRequest req,
			HttpServletResponse resp, String categoryid, Integer featureid) {
		feature f = new feature();
		f.setId(featureid);
		f.setCateId(categoryid);
		featureDao.updateByPrimaryKeySelective(f);
		return "{\"code\":0,\"msg\":\"\",\"data\":true}";
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/copy", method = RequestMethod.POST)
	public String feature_copy(HttpServletRequest req,
			HttpServletResponse resp, Integer id) {
		feature f = new feature();
		if(id>0){
			f.setId(id);
			featureDao.copy(f);
			String realPath = getRealPath(req, "/data");
			JsonToFile.savefeature(f.getId().toString(), JsonToFile.getfeaturejson(id.toString() , realPath),realPath);
		}
		return "{\"code\":0,\"msg\":\"\",\"data\":\""+f.getId().toString()+"\"}";
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/homepage", method = RequestMethod.POST)
	public String feature_homepage(HttpServletRequest req,
			HttpServletResponse resp, Integer id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("isHomepage", 0);
		params.put("storeId", 1);
		featureDao.updateByMap(params);
		params.put("isHomepage", 1);
		params.put("id", id);
		featureDao.updateByMap(params);
		return "{\"code\":0,\"msg\":\"\",\"data\":[]}";
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/item", method = RequestMethod.POST)
	public String feature_item_post(HttpServletRequest req,
			HttpServletResponse resp, String data,Integer is_display) {
		
		JsonParser jsonParse = new JsonParser();
		JsonObject json = new JsonObject();
		JsonArray jsona = new JsonArray();
		jsona = (JsonArray) jsonParse.parse(data);
		json = (JsonObject) jsona.get(0);
		feature f = new feature();
		String alias = new Long(System.nanoTime()).toString();
		f.setTitle(json.get("title").getAsString());
		f.setDescription(json.get("description").getAsString());
		f.setAlias(alias);
		f.setCreatedTime(Integer.valueOf(new Long(new Date().getTime())
				.toString().substring(0, 10)) );
		JsonArray categoryJ = (JsonArray) json.get("category");
		String category = categoryJ.size()>0?categoryJ.get(0).getAsString():null;
		StringBuilder sb = new StringBuilder();
		for(JsonElement je:categoryJ){
			sb.append(je.getAsString());
			sb.append(",");
		}
		f.setCateId(category==null?"":String.valueOf(sb.subSequence(0, sb.length()-1)));
		f.setNum(0);
		f.setGoodsNum(0);
		f.setBroUvpv(0);
		f.setIsHomepage(false);
		f.setShopUvpv("0");
		f.setIsDisplay(is_display==1);
		f.setStoreId(1);
		featureDao.insert(f);
		Integer id = f.getId();
		if(id>0){
			JsonToFile.savefeature(id.toString(), data, getRealPath(req, "/data"));
		}
		if(StringUtils.hasText(category)){
			featureclassDao.addCountByids(category.split(","));
		}
		json = new JsonObject();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject jsondata = new JsonObject();
		jsondata.addProperty("alias", alias);
		jsondata.addProperty("id", id.toString());
		json.add("data",jsondata);
		//'{"code":0,"msg":"","data":{"alias":"'.$alias.'","id":"'.$id.'"}}';
		return json.toString();
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/item", method = RequestMethod.PUT)
	public String feature_item_put(HttpServletRequest req,
			HttpServletResponse resp, String data, String category,
			String alias, Integer id,Integer is_display) {
		JsonParser jsonParse = new JsonParser();
		JsonObject json = new JsonObject();
		JsonArray jsona = new JsonArray();
		jsona = (JsonArray) jsonParse.parse(data);
		json = (JsonObject) jsona.get(0);
		if(id>0){
			json.addProperty("goods_id", id);
			JsonToFile.savefeature(id.toString(), data, getRealPath(req, "/data"));
		}
		feature f = new feature();
		f.setIsDisplay(is_display==1);
		f.setCateId(category);
		f.setTitle(json.get("title").getAsString());
		f.setDescription(json.get("description").getAsString());
		f.setId(id);
		featureDao.updateByPrimaryKeySelective(f);
		json = new JsonObject();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject jsondata = new JsonObject();
		jsondata.addProperty("alias", alias);
		jsondata.addProperty("id", id.toString());
		json.add("data",jsondata);
		//'{"code":0,"msg":"","data":{"alias":"'.$alias.'","id":"'.$id.'"}}';
		return json.toString();
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/item", method = RequestMethod.DELETE)
	public String feature_item_delete(HttpServletRequest req,
			HttpServletResponse resp, Integer id,String[] ids) {
		List<Integer> idList = new ArrayList<Integer>();
		if(id!=null){
			idList.add(id);
		}
		if(ids!=null){
			for(String idCu:ids){
				idList.add(Integer.valueOf(idCu));
			}
		}
		for(int i =0;i<idList.size();i++){
			feature f = featureDao.selectByPrimaryKey(idList.get(i));
			featureDao.deleteByPrimaryKey(idList.get(i));
			JsonToFile.deletefeaturejson(idList.get(i).toString(), getRealPath(req, "/data"));
			if(StringUtils.hasText(f.getCateId())){
				featureclassDao.reduceCountByIds(f.getCateId().split(","));
			}
		}
		return "{\"code\":0,\"msg\":\"\",\"data\":[]}";
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/item", method = RequestMethod.GET)
	public String feature_item_get(HttpServletRequest req,
			HttpServletResponse resp, Integer id) {
		feature f = featureDao.selectByPrimaryKey(id);
		JsonObject json = new JsonObject();
		JsonParser jsonp = new JsonParser();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject data = new JsonObject();
		data.addProperty("id", f.getId().toString());
		data.addProperty("alias", f.getAlias());
		data.addProperty("is_display", "1");
		data.add("data", jsonp.parse(JsonToFile.getfeaturejson(f.getId().toString(), getRealPath(req,"/data"))));
		json.add("data",data );
		return json.toString();
	}
	
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/num", method = RequestMethod.GET)
	public String feature_num(HttpServletRequest req,
			HttpServletResponse resp, Integer id, Integer num) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("num",num);
		params.put("id", id);
		params.put("storeId", 1);
		featureDao.updateByMap(params);
		return "{\"code\":0,\"msg\":\"\",\"data\":"+num.toString()+"}";
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/feature/shortList", method = RequestMethod.GET)
	public String feature_shortList(HttpServletRequest req,
			HttpServletResponse resp, Integer is_display, Integer category,
			String orderby, String order, String keyword) {
		
		return "";
	}
	
	
	// /models/showcase/feature/list.php?is_display=1&keyword=&p=1&orderby=created_time&order=desc&_=1451204217729
	@ResponseBody
	@RequestMapping(value = "showcase/feature/list", method = RequestMethod.GET)
	public String feature_list(HttpServletRequest req,
			HttpServletResponse resp, Integer is_display, Integer category,
			String orderby, String order, String keyword,Integer p) {
		JsonObject json = new JsonObject();
		JsonObject data = new JsonObject();
		JsonArray list = new JsonArray();
		JsonObject homepages = new JsonObject();
		is_display = is_display == null ? 1 : is_display;
		List<String> orderbys = new ArrayList<String>();
		orderbys.add("created_time");
		orderbys.add("is_homepage");
		orderbys.add("goods_num");
		orderbys.add("num");
		orderby = (orderby != null && orderbys.contains(orderby)) ? orderby
				.trim() : "created_time";
		keyword = keyword.trim();
		// $sql="select * from ".$bidcmstable_prefix."feature where is_display=".$is_display." and store_id=".$bidcms_store_id;
		Map<String, Object> params = new HashMap<String, Object>();
		category = category == null ? 0 : category;
		if (category > 0) {
			params.put("category", category);
			// $sql.=" and  INSTR(CONCAT(',',cate_id,','), ',".$cate.",')>0";
		}
		if (StringUtils.hasText(keyword)) {
			params.put("keyword", keyword);
			// $sql.=" and  title like '%".$keyword."%'";
		}
		params.put("orderby", orderby);
		params.put("order", order);
		params.put("is_display", is_display);
		params.put("p", p);
		params.put("store_id", 1);
		// $sql.=" order by ".$orderby." ".$order;
		long total = featureDao.queryPage(params);
		PageHtmlStrUtil phs = new PageHtmlStrUtil(total,p,20);
		addPageParams(p, 20, params);
		List<feature> features = featureDao.findByMap(params);
		for (feature f : features) {
			JsonObject item = new JsonObject();
			
			item.addProperty("title", f.getTitle());
			item.addProperty("num", f.getNum().toString());
			item.addProperty("goods_num", f.getGoodsNum().toString());
			item.addProperty("alias", f.getAlias());
			item.addProperty("bro_uvpv", f.getBroUvpv().toString());
			item.addProperty("shop_uvpv", f.getShopUvpv().toString());
			item.addProperty("created_time", MyDateUtil.IntegerToDateString(f.getCreatedTime()));
			if (f.getIsHomepage()) {
				item.addProperty("id", f.getId().toString());
				item.addProperty("url", getFreeMarkerVar("webRoot") + "/index?con=show");
				homepages = item;
			}
			item.addProperty("item_id", f.getId().toString());
			item.addProperty("url", getFreeMarkerVar("webRoot")
					+ "/index?con=show&act=feature&id=" + f.getId());
			item.addProperty("is_homepage", f.getIsHomepage() ? "1" : "0");
			JsonArray categories = new JsonArray();
			if (!f.getCateId().equals("")) {
				String[] cates = f.getCateId().split(",");
				JsonParser parser = new JsonParser();
				// 通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
				for (String cate : cates) {
					JsonElement catej = parser.parse(cate);
					categories.add(catej);
				}
			}
			item.add("categories", categories);
			list.add(item);
		}
		data.add("homepage", homepages);
		data.add("list", list);
		data.addProperty("page", phs.displayForPage());
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		json.add("data", data);
		return json.toString();
	}

	@ResponseBody
	@RequestMapping(value = "showcase/component/item", method = RequestMethod.GET)
	public String component_get(HttpServletRequest req,
			HttpServletResponse resp,int id) {
		JsonObject json = new JsonObject();
		JsonParser jsonp = new JsonParser();
		component c= componentDao.selectByPrimaryKey(id);
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject data = new JsonObject();
		data.addProperty("id", c.getId().toString());
		data.addProperty("title", c.getTitle());
		data.addProperty("alias", c.getAlias());
		data.add("data", jsonp.parse(JsonToFile.getcomponentjson(c.getId().toString(), getRealPath(req,"/data"))));
		json.add("data",data );
		return json.toString();
	}

	@ResponseBody
	@RequestMapping(value = "showcase/component/item", method = RequestMethod.POST)
	public String component_post(HttpServletRequest req,
			HttpServletResponse resp,String data,String title,int is_display) {
		JsonParser jsonParse = new JsonParser();
		JsonObject json = new JsonObject();
		JsonArray jsona = new JsonArray();
		jsona = (JsonArray) jsonParse.parse(data);
		json = (JsonObject) jsona.get(0);
		component c = new component();
		String alias = Long.toHexString(new Long(System.nanoTime())).substring(0,10);
		c.setTitle(title);
		c.setAlias(alias);
		c.setIsDefault((byte) 0);
		c.setApplyNum(0);
		c.setApplyList("");
		c.setCreatedTime(Integer.valueOf(new Long(new Date().getTime())
				.toString().substring(0, 10)) );
		c.setStoreId(1);
		componentDao.insert(c);
		Integer id = c.getId();
		if(id>0){
			JsonToFile.savecomponent(id.toString(), data, getRealPath(req, "/data"));
		}
		json = new JsonObject();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject jsondata = new JsonObject();
		jsondata.addProperty("alias", alias);
		jsondata.addProperty("id", id.toString());
		json.add("data",jsondata);
		//'{"code":0,"msg":"","data":{"alias":"'.$alias.'","id":"'.$id.'"}}';
		return json.toString();
	}

	@ResponseBody
	@RequestMapping(value = "showcase/component/item", method = RequestMethod.PUT)
	public String component_put(HttpServletRequest req,
			HttpServletResponse resp,Integer id,String data,String title,int is_display,String alias) {
		component c = new component();
		c.setTitle(title);
		c.setId(id);
		c.setStoreId(1);
		componentDao.updateByPrimaryKeySelective(c);
		JsonToFile.savecomponent(id.toString(), data, getRealPath(req, "/data"));
		JsonObject json = new JsonObject();
		json.addProperty("code", 0);
		json.addProperty("msg", "");
		JsonObject jsondata = new JsonObject();
		jsondata.addProperty("alias", alias);
		jsondata.addProperty("id", id.toString());
		json.add("data",jsondata);
		//'{"code":0,"msg":"","data":{"alias":"'.$alias.'","id":"'.$id.'"}}';
		return json.toString();
	}

	@ResponseBody
	@RequestMapping(value = "showcase/component/item", method = RequestMethod.DELETE)
	public String component_delete(HttpServletRequest req,
			HttpServletResponse resp,Integer id,String[] ids) {
		List<Integer> idList = new ArrayList<Integer>();
		if(id!=null){
			idList.add(id);
		}
		if(ids!=null){
			for(String idCu:ids){
				idList.add(Integer.valueOf(idCu));
			}
		}
		for(int i =0;i<idList.size();i++){
			componentDao.deleteByPrimaryKey(idList.get(i));
			JsonToFile.deletecomponentjson(idList.get(i).toString(), getRealPath(req, "/data"));
		}
		return "{\"code\":0,\"msg\":\"\",\"data\":0}";
	}

	@ResponseBody
	@RequestMapping(value = "showcase/component/list", method = RequestMethod.GET)
	public String component_list(HttpServletRequest req,
			HttpServletResponse resp,String orderby, String order, String keyword,Integer p) {
		JsonObject data = new JsonObject();
		JsonArray jarr = new JsonArray();
		int bidcms_store_id = 1;
		if (bidcms_store_id > 0) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("storeId", "1");// 商铺ID
			param.put("orderby", orderby);
			param.put("order", order);
			if (StringUtils.hasText(keyword)) {
				param.put("keyword", keyword);
				// $sql.=" and  title like '%".$keyword."%'";
			}
			param.put("p", p);
			long total = componentDao.queryPage(param);
			PageHtmlStrUtil phs = new PageHtmlStrUtil(total,p,20);
			addPageParams(p, 20, param);
			List<component> items = componentDao.findByMap(param);
			for (component comp : items) {
				JsonObject item = new JsonObject();
				item.addProperty("item_id", comp.getId());
				item.addProperty("title", comp.getTitle());
				item.addProperty("is_default", comp.getIsDefault());
				item.addProperty("apply_num", comp.getApplyNum());
				item.addProperty("apply_list", comp.getApplyList());
				item.addProperty("url", comp.getAlias());
				item.addProperty("created_time", MyDateUtil.IntegerToDateString(comp
								.getCreatedTime()));
				jarr.add(item);
			}

			// "select * from ".$bidcmstable_prefix."component where store_id=".$bidcms_store_id);
			// $list=array();
			// while($rows=mysqli_fetch_assoc($query))
			// {
			// $list[]=array('item_id'=>$rows['id'],'title'=>$rows['title'],'is_default'=>$rows['is_default'],'apply_num'=>$rows['apply_num'],'apply_list'=>$rows['apply_list'],'url'=>$rows['alias'],'created_time'=>date('Y-m-d
			// H:i:s',$rows['created_time']));
			// }
			data.addProperty("page",
					phs.displayForPage());
			data.add("list", jarr);
		}
		JsonObject jsonf = new JsonObject();
		jsonf.add("data", data);
		jsonf.addProperty("code", 0);
		jsonf.addProperty("msg", "");
		return jsonf.toString();
	}
	
	@ResponseBody
	@RequestMapping(value = "showcase/component/point")
	public String component_point(HttpServletRequest req,
			HttpServletResponse resp) {

		return "{\"code\":0,\"msg\":\"\",\"data\":{\"point\":\"0\",\"level\":\"\u5ba2\u6237\"}";
	}

	@ResponseBody
	@RequestMapping(value = "showcase/component/title",method=RequestMethod.PUT)
	public String component_title(HttpServletRequest req,
			HttpServletResponse resp,Integer id,String title) {
		component c = new component();
		c.setId(id);
		c.setTitle(title);
		c.setStoreId(1);
		componentDao.updateByPrimaryKeySelective(c);
		return "{\"code\":0,\"msg\":\"\",\"data\":1}";
	}

	@ResponseBody
	@RequestMapping(value = "showcase/tag/special",method=RequestMethod.GET)
	public String tag_special(){
		return "{\"code\":0,\"msg\":\"\",\"data\":[{\"id\":\"4774765\",\"title\":\"\u5176\u4ed6\",\"alias\":\"other\",\"is_default\":\"1\",\"url\":\"http://wap.koudaitong.com/v2/showcase/tag?alias=other\"},{\"id\":\"4774766\",\"title\":\"\u6700\u65b0\",\"alias\":\"new\",\"is_default\":\"2\",\"url\":\"http://wap.koudaitong.com/v2/showcase/tag?alias=new\"},{\"id\":\"4774767\",\"title\":\"\u6700\u70ed\",\"alias\":\"hot\",\"is_default\":\"3\",\"url\":\"http://wap.koudaitong.com/v2/showcase/tag?alias=hot\"},{\"id\":\"4774768\",\"title\":\"\u5217\u8868\u4e2d\u9690\u85cf\",\"alias\":\"hide\",\"is_default\":\"4\",\"url\":\"http://wap.koudaitong.com/v2/showcase/tag?alias=hide\"}]}";
	}
	
	//attachment/alert
	@ResponseBody
	@RequestMapping(value = "showcase/attachment/alert",method=RequestMethod.GET)
	public String attachment_alert(HttpServletRequest req,
			HttpServletResponse resp){
		//select count(id) as total from attachment where user_id=1;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("storeId",1);
		long total = attachmentDao.queryCount(params);
		List<attachment> atttachments=  attachmentDao.findByMap(params);
		JsonArray items = new JsonArray();
		JsonObject data = new JsonObject();
		//{"errcode":0,"errmsg":"","data":{"data_list":[],"page_view":"<span class=\"total\">\u5171  \u6761\uff0c\u6bcf\u9875 27 \u6761<\/span>","total_items":null}}
		for(attachment atth : atttachments){
			JsonObject item =new JsonObject();
			item.addProperty("attachment_id", atth.getId());
			item.addProperty("attachment_title", atth.getTitle());
			item.addProperty("attachment_url", atth.getUrl());
			item.addProperty("attachment_size", atth.getSize());
			item.addProperty("created_time", MyDateUtil.IntegerToDateString(atth.getCreatedTime()));
			item.addProperty("thumb_url", atth.getThumbUrl());
			item.addProperty("media_type", atth.getMediaType());
			item.addProperty("width", atth.getWidth());
			item.addProperty("height", atth.getHeight());
			item.addProperty("attachment_full_url", getFreeMarkerVar("webRoot")+"/"+atth.getUrl());
			items.add(item);
		}
		data.add("data_list", items);
		data.addProperty("page_view", "<span class=\"total\">共"+total+" 条，每页 27 条</span>");
		data.addProperty("total_items", total);
		JsonObject json = new JsonObject();
		json.addProperty("errcode", 0);
		json.addProperty("errmsg", "");
		json.add("data", data);
		//select * from ".$bidcmstable_prefix."attachment where user_id=".$bidcms_uid;
		return json.toString();
	}
	
	
	
	public static void main(String[] args) {
		/*
		 * JsonParser parser = new JsonParser();
		 * 
		 * //通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象 JsonArray categories
		 * = new JsonArray(); String cates[] = new String[]{"a","b","c"};
		 * for(String cate : cates){ JsonElement catej = parser.parse(cate);
		 * categories.add(catej); } System.out.println(categories.toString());
		 */
		List<String> strs = new ArrayList<String>();
		strs.add("a");
		strs.add("b");
		strs.add("c");
		System.out.println(strs);
		String lx = Long.toHexString(new Long(System.nanoTime())).substring(0,10);
		System.out.println(lx);
		lx = Long.toHexString(new Long(System.nanoTime())).substring(0,10);
		System.out.println(lx);
		lx = Long.toHexString(new Long(System.nanoTime())).substring(0,10);
		System.out.println(lx);
		lx = Long.toHexString(new Long(System.nanoTime())).substring(0,10);
		System.out.println(lx);
		lx = Long.toHexString(new Long(System.nanoTime())).substring(0,10);
		System.out.println(lx);
	}
}
